<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Movie_Reviews extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by='created_date', $sort_order='asc', $current_page=0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					//echo '<pre>';print_r($search_by_value);exit;
					}
				}
			$results_per_page = 3;
		
		
			$data = array('www-command'=>'reviews-all_reviews', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order, 'search_by'=>$search_by, 'search_by_value'=>$search_by_value);
			$response = $this->get_review_list($data);
			//echo '<pre>';print_r($response);exit;
			
			if($response->message !== 'No Review'){
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'admin/movie_reviews/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['reviews_all_reviews'] = $response;
			$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['reviews_all_reviews'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['sort_order'] = $sort_order;
			$data['order_by'] = $order_by;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$data['review_fields'] = array('movie_name'=>'Movie Name', 'language'=>'Language', 'movie_type'=>'Type', 'author'=>'Author', 'created_date'=>'Date');
			$this->load->view('admin/movie_review', $data);
 		}
		
		function search_by(){
			
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$this->input->post('search')
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/movie_reviews/index/'.$query_id);
			
		}
		
		function create_review(){
			
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('event_id', '', 'callback_moviename');
				$this->form_validation->set_rules('author', 'Author', 'required');
				$this->form_validation->set_rules('language', '', 'callback_language');
				$this->form_validation->set_rules('movie_type', '', 'callback_movietype');
				$this->form_validation->set_rules('description', 'Description', 'required');
				
				if($this->form_validation->run() == FALSE){
						
				$data = array('www-command'=>'reviews-get_events_list');
				$get_events_list = $this->get_review_list($data);
				$data['get_events_list'] = $get_events_list;
				$data['language_fields'] = array('hindhi'=>'Hindhi', 'telugu'=>'Telugu', 'tamil'=>'Tamil', 'marathi'=>'Marathi', 'malayalam'=>'Malayalam', 'gujarati'=>'Gujarati', 'bengali'=>'Bengali', 'kannada'=>'Kannada');
				$this->load->view('admin/create_review_page', $data);
				}else{
				$filesupload = array('tmp_name'=>$_FILES['image_name']['tmp_name'],'name'=>$_FILES['image_name']['name']);	
				//echo $this->input->post('event_id');exit;
				$data = array('www-command'=>'reviews-create_review', 
					'event_id' => $this->input->post('event_id'),
					'language' => $this->input->post('language'),
					'movie_type' => $this->input->post('movie_type'),
					'author' => $this->input->post('author'),
					'description' => $this->input->post('description'),
					'review_image'=>$filesupload,
					'review_url'=>$this->input->post('review_url')
					);
					//echo '<pre>';print_r($data);exit;
					$response = $this->get_review_list($data);
					//echo '<pre>';print_r($response);exit;
					redirect('admin/movie_reviews/index/');
				}
			}
			function moviename($moviename){
			//echo $articletype;exit;
			if($moviename == ""){
				$this->form_validation->set_message('moviename','Please Select movie name type');
				return false;
			}else{
				return true;
			}
		}function language($language){
			//echo $articletype;exit;
			if($language == ""){
				$this->form_validation->set_message('language','Please Select Language type');
				return false;
			}else{
				return true;
			}
		}function movietype($movietype){
			//echo $movietype;exit;
			if($movietype == ""){
				$this->form_validation->set_message('movietype','Please Select Movie type');
				return false;
			}else{
				return true;
			}
		}
			
			
			
			 function get_review_list($data){
				$url = CURL_URL;
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				return $response;
			}
			
			function edit_review(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('event_id', '', 'callback_moviename');
				$this->form_validation->set_rules('author', 'Author', 'required');
				$this->form_validation->set_rules('language', '', 'callback_language');
				$this->form_validation->set_rules('movie_type', '', 'callback_movietype');
				$this->form_validation->set_rules('description', 'Description', 'required');
			
				if($this->form_validation->run() == FALSE){
						if($this->uri->segment(4) > 0){
					$_SESSION['review_id'] = $this->uri->segment(4);
				}
					$data = array('www-command'=>'events-all_review', 
					'review_id' => $_SESSION['review_id']
					);
					$get_event_location = $this->get_review_list($data);
					
					$data = array('www-command'=>'reviews-get_events_list');
					$get_events_list = $this->get_review_list($data);
					$data['get_events_list'] = $get_events_list;
					$data['language_fields'] = array('hindhi'=>'Hindhi', 'telugu'=>'Telugu', 'tamil'=>'Tamil', 'marathi'=>'Marathi', 'malayalam'=>'Malayalam', 'gujarati'=>'Gujarati', 'bengali'=>'Bengali', 'kannada'=>'Kannada');
					//echo '<pre>';print_r($data['get_event_location']);exit;
					$this->load->view('admin/edit_review_page', $data);
			
					
				}else{
					$filesupload = array('tmp_name'=>$_FILES['image_name']['tmp_name'],'name'=>$_FILES['image_name']['name']);	
					$data = array('www-command'=>'reviews-edit_review', 
					'event_id' => $this->input->post('event_id'),
					'language' => $this->input->post('language'),
					'movie_type' => $this->input->post('movie_type'),
					'author' => $this->input->post('author'),
					'description' => $this->input->post('description'),
					'review_image'=>$filesupload,
					'review_url'=>$this->input->post('review_url'),
					'review_id'=>$_SESSION['review_id']
					);
					//echo '<pre>';print_r($data);exit;
					$response = $this->get_review_list($data);
					//echo '<pre>';print_r($response);exit;
					redirect('admin/movie_reviews/index/');
				}
			}
			
				function delete_review(){
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'reviews-delete_review','status'=>$status, 'review_id'=>$this->uri->segment(4));
				$response = $this->get_review_list($data);
				//echo '<pre>';print_r($response);exit;
				redirect('admin/movie_reviews/index/');
			}
}